<?php
namespace app\index\controller;
use think\Controller;
use think\facade\Cache;
use think\facade\Session;
use app\common\model\User;
class Login extends Controller
{
    public function index()
    {
        //判断是否登录
        if($this->isLogin()) {
            return $this->redirect('/index');
        }

        if(request()->isAjax()){

            $model = new User();

            $param = input("param.");

            $mobile = $param["phoneNum"];

            $password = $param["password"];

            $where["mobile"] = $mobile;

            if($param["type"]==1){

                $where["password"] = md5(md5($password).md5(config("wap.wapuser_password_halt")));
            
                $user = $model->where($where)->find();
                
                if(empty($user))
                    return index_json(-1, "账号不存在或密码不正确！", "");

                Session::set(config("wap.session_wapuser"), $user);
            
                return index_json(1, "成功！" , "", url("/my"));
            }

            // 驗證碼登陸
            if($param["type"]==2){

                $yzm = $param["yzm"];

                $read = Cache::get($mobile."read","");
            
                if($read==""){
                    return index_json(0, "无效验证码！" , "");
                    die;
                }
                if($read!=$yzm){
                    return index_json(-2, "验证码不正确！" , "");
                    die;
                }

                $user_mobile = $model->where($where)->find();

                // 直接登陸
                if(!empty($user_mobile)){

                    Session::set(config("wap.session_wapuser"), $user_mobile);
            
                    return index_json(1, "成功！" , "", url("/my"));

                }else{

                    $data["mobile"] = $mobile;

                    $data["is_member"] = 1;

                    try{
                        
                        $id = $model->addData($data);

                        Cache::rm($mobile."read");

                        $user = $model->where("id",$id)->find();

                        Session::set(config("wap.session_wapuser"), $user);

                        return index_json(1, "成功！" , "", url("/my"));

                    }catch (\Exception $e){
                        return index_json(-3, "失败！".$e->getMessage() , "");
                    }
                }
            }    
        }

		return $this->fetch();
    }

    // 注册
    public function register()
    {
    	if(request()->isAjax()){

    		$model = new User();

    		$param = input("param.");

    		$mobile = $param["phoneNum"];

    		$yzm = $param["yzm"];

    		$read = Cache::get($mobile."read","");
    		
    		if($read==""){
                return index_json(0, "无效验证码！" , "");
                die;
            }
            if($read!=$yzm){
                return index_json(-1, "验证码不正确！" , "");
                die;
            }
            if($param["password1"]!=$param["password2"]){
            	return index_json(-2, "两次输入密码不一致！" , "");
                die;
            }

            $data["mobile"] = $mobile;
            $data["is_member"] = 1;
            $data["password"] = md5(md5($param["password1"]).md5(config("wap.wapuser_password_halt")));

            try{
            	
            	$result = $model->where(["mobile"=>$mobile])->find();

            	if(!empty($result))
            		return index_json(-3, "该手机号已注册过！", "");
            	
                $model->addData($data);

                Cache::rm($mobile."read");

                return index_json(1, "成功！" , "", url("/waplogin"));

            }catch (\Exception $e){
                return index_json(-3, "失败！".$e->getMessage() , "");
            }
    	}

    	return $this->fetch();
    }

    /**
     * [isLogin  判定是否登录]
     * @author [TRC南風]
     * @return bool
     */
    public function isLogin() {

        //获取session
        $user = Session::has(config("wap.session_wapuser"));

        if($user==true) {

            return true;
        }

        return false;
    }

    /**
     * [loginOut  退出登录]
     * @author [TRC南風]
     */
    public function loginOut()
    {

        Session::clear();

        Session::clear(config("wap.session_wapuser"));

        $this->redirect('/index');
    }
}
